<template>
  <el-card class="role-card">
    <div class="home-box">
      <el-form :inline="true" :model="searchForm" v-show="showSearch">
        <el-form-item label="职务名称">
          <el-input
            v-model="searchForm.roleName"
            placeholder="职务名称"
            clearable
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="创建时间" style="width: 18.75rem">
          <el-date-picker
            v-model="betweenTime"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item> -->
        <el-form-item>
          <el-button @click="searchEvent" type="primary">查询 </el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="searchEvent" type="primary">重置查询 </el-button>
        </el-form-item>
      </el-form>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getRoleListA"
        :haveImport="false"
        @onOutClick="onDownTemplate"
        @onAddClick="onDownTemplate"
      >
        <template #left-btn>
          <el-row :gutter="10">
            <el-col :span="1.5">
              <addRole @getRoleList="getRoleListA" :selection="selection" />
            </el-col>
            <el-col :span="1.5">
              <updateRole @getRoleList="getRoleListA" :selection="selection" />
            </el-col>
            <el-col :span="1.5">
              <deleteRole @getRoleList="getRoleListA" :selection="selection" />
            </el-col>
          </el-row>
        </template>
      </right-toolbar>
      <el-table
        :data="tableData"
        v-loading="loading"
        element-loading-text="加载中..."
        :border="true"
        @selection-change="selectChange"
      >
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column
          prop="roleName"
          label="职务名称"
          width="200"
          header-align="center"
          align="center"
        >
        </el-table-column>

        <el-table-column label="状态" width="160" align="center">
          <template #default="{ row }">
            <el-tag :type="row.state == '0' ? 'danger' : 'success'">{{
              row.state == "0" ? "停用" : "正常"
            }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" min-width="160" align="center">
          <template #default="{ row }">
            {{ $filters.dateFilter(row.operator, "YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          min-width="160"
          align="center"
        >
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="assignUser(row)"
              >授权员工
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="searchForm.pageNum"
        v-model:limit="searchForm.pageSize"
        :pageSizes="[10, 20, 30, 40]"
        @pagination="getRoleListA"
      />
    </div>
  </el-card>
</template>
<script>
export default {
  name: "getRoleList",
};
</script>
<script setup>
import updateRole from "./components/updateRole.vue";
import addRole from "./components/addRole.vue";
import deleteRole from "./components/deleteRole.vue";
import eventBus from "@/utils/event-bus";
import { ref, onMounted } from "vue";
import { getRoleList } from "@/service/api/role";
import { useRouter } from "vue-router";

const showSearch = ref(true);
const selection = ref([]);
const router = useRouter();
const roleId = ref();
const roleName = ref();

onMounted(() => {
  getRoleListA();
});

//数据源
const searchForm = ref({
  roleId: null,
  roleName: "",
  pageNum: 1,
  pageSize: 10,
  beginTime: "",
  endTime: "",
});

const tableData = ref([]);
const total = ref(0);
const loading = ref(false);
const betweenTime = ref([]);

/**
 * 获取角色列表
 */
const getRoleListA = async () => {
  if (betweenTime.length !== 0) {
    const [beginTime, endTime] = betweenTime.value;
    searchForm.value.beginTime = beginTime;
    searchForm.value.endTime = endTime;
  }
  // loading.value = true;
  const { count, rows } = await getRoleList(searchForm.value);
  total.value = count;
  tableData.value = rows;
  // loading.value = false;
};

/**
 * 勾选表格CheckBox的回调函数-获取勾选的数组
 */
const selectChange = (select) => {
  selection.value = select;
};

const assignUser = (row) => {
  router.push({
    path: "/emp/accredit",
    query: {
      roleId: row.id,
      roleName: row.roleName,
    },
  });
};

const searchEvent = () => {
  searchForm.value.page = 1;
  getListData();
};

// eventBus.on("getRoleList", () => {
//   getRoleListA();
// });
</script>

<style lang="scss" scoped>
.role-card {
  .home-box {
    .header {
      margin-bottom: 22px;
      text-align: right;
    }

    :deep(.el-tag) {
      margin-right: 6px;
    }

    .pagination {
      // margin-top: 20px;
      margin: 0 auto;
    }
  }
}
.el-card {
  border-radius: 10px;
}
</style>
